package com.atguigu.gulimall.authserver.web;

import com.atguigu.gulimall.authserver.vo.UserRegistVO;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class indexController {

    @RequestMapping("/reg.html")
    public String reg() {
        return "reg";
    }


    @PostMapping("/register")
    public ModelAndView register(UserRegistVO vo, BindingResult result, ModelAndView modelAndView) {
        System.out.println(vo);
        if (result.hasErrors()) {
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            modelAndView.addObject("errors",errors);
            modelAndView.setViewName("reg");
            return modelAndView;
        }
        modelAndView.setViewName("redirect:/login.html");
        return modelAndView;
    }
}
